﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;
using System.Resources;
using System.Globalization;

namespace LPV_POS.mh_pos.mh_cauhinh
{
    public partial class mh_dangnhap : DevExpress.XtraEditors.XtraForm
    {
        CultureInfo culture;
        string str_thongbao = "";
        string str_lang = "";
        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);
                //lblVAT_Co.Text = rm.GetString("vat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion

        public mh_dangnhap()
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
        }

        private void mh_dangnhap_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);


            string sSQL = "SELECT * FROM DM_DANGNHAP";
            thuvien othuvien = new thuvien();
            DataTable tbDM_CA_LAM_VIEC = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            //othuvien.f_showdataoncombox(cboCaLamViec, tbDM_CA_LAM_VIEC, "OBJCODE", "OBJNAME");

            //if (tbDM_CA_LAM_VIEC.Rows.Count > 0)
            //{
            //    int ipos = -1;
            //    for (ipos = 0; ipos < tbDM_CA_LAM_VIEC.Rows.Count; ipos++)
            //    {
            //        TimeSpan tp1 = ((DateTime)tbDM_CA_LAM_VIEC.Rows[ipos]["TU_GIO"]).TimeOfDay;
            //        TimeSpan tp2 = ((DateTime)tbDM_CA_LAM_VIEC.Rows[ipos]["DEN_GIO"]).TimeOfDay;
            //        if (tp1 <= DateTime.Now.TimeOfDay && DateTime.Now.TimeOfDay <= tp2)
            //        {
            //            //cboCaLamViec.SelectedIndex = ipos;
            //            //cboCaLamViec.Text = tbDM_CA_LAM_VIEC.Rows[ipos]["OBJNAME"] + "";
            //            break;
            //        }
            //    }
                    
            //}
            txtUsername.Text = new cpublic().Registry_Get(this.Name + txtUsername.Name);
            //cboCaLamViec.Text = new cpublic().Registry_Get(this.Name + cboCaLamViec.Name);
            this.Hide();
            mh_so omh_so = new mh_so(true);
            omh_so.eClick += new EventHandler(DangNhap);
            omh_so.ShowDialog();
            this.Dispose();
            
        }
        void DangNhap(object sender, EventArgs e)
        {
            try
            {
                mh_so omh_so = (mh_so)sender;
                txtUsername.Text = omh_so.txt_ketqua.Text.Trim();

                this.bton_DangNhap_Click(null, null);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        private void bton_1_Click(object sender, EventArgs e)
        {
            txtUsername.Text = new mh_chu().ShowForm("", this.Location.X - 100, this.Location.Y - 100, false);
        }

        private void bton_2_Click(object sender, EventArgs e)
        {
            txtPwd.Text = new mh_chu().ShowForm("", this.Location.X - 100, this.Location.Y - 100, true);
        }

        private void bton_DangNhap_Click(object sender, EventArgs e)
        {
            //string _spwd = new cpublic().MaHoa(txtUsername.Text, txtPwd.Text);
            //            
            string sSQL = "SELECT * FROM DM_DANGNHAP";
            thuvien othuvien = new thuvien();
            DataTable tbDM_DANGNHAP = othuvien.SP_SelectDataTable("SP_EXECUTESQL",
                                                                    new string[] { "@sSQl"},
                                                                    new object[] { sSQL});
            
            if (tbDM_DANGNHAP.Select("USERNAME LIKE '" + txtUsername.Text + "'").Length > 0)
            {
                DataRow dr = tbDM_DANGNHAP.Select("USERNAME LIKE '" + txtUsername.Text + "'")[0];
                
                //kiem tra da RA CA hay chua?
                sSQL = "SELECT 1 "+
                       "FROM BH_LICHSU_CALAMVIEC "+
                       "WHERE ngay_gio_ra IS NULL "+
                       "    AND ca_ma = '" + dr["USERNAME"] + "' "+
                       "    AND TEN_MAY_TINH = '" + SystemInformation.ComputerName + "'";

                DataTable tb = othuvien.SP_SelectDataTable("SP_EXECUTESQL", 
                                                            new string[] { "@sSQL"}, 
                                                            new object[] { sSQL});
                if (tb.Rows.Count > 0)
                {
                    switch (str_lang)
                    {
                        case "vi-VN":
                            str_thongbao = "Bạn chưa thoát ca làm việc.";
                            break;
                        case "en-US":
                            str_thongbao = "You haven't gone out yet";
                            break;
                    }

                    MessageBox_LPV.Show(str_thongbao);
                    return;
                }

                //Variable.username = dr["USERNAME"] + "";
                //Variable.calamviec = dr["OBJCODE"] + "";
                //Variable.calamviec_ten = dr["OBJNAME"] + "";
                //Variable.ca_raca_vaoca = dr["USERNAME"] + "";

                othuvien.SP_Update("sp_BH_LICHSU_CALAMVIEC",
                                    new string[] { "@flag", "@ca_ma", "@ca_ten", "@TEN_MAY_TINH" },
                                    new object[] { 1, dr["USERNAME"], dr["OBJNAME"], SystemInformation.ComputerName });

                Variable.iquyen = int.Parse("0" + dr["IQUYEN"]);

                this.DialogResult = DialogResult.OK;
                new cpublic().Load_tbBH_CAU_HINH();

                switch (str_lang)
                {
                    case "vi-VN":
                        str_thongbao = "Thành công.";
                        break;
                    case "en-US":
                        str_thongbao = "Successful.";
                        break;
                }
                MessageBox_LPV.Show(str_thongbao);
                this.Dispose();
            }
            else
            {
                //Variable.username = "";
                //Variable.calamviec = "";
                new mh_thongbao("Đăng nhập thất bại", 2).ShowDialog();
            }
            new cpublic().Registry_Set(this.Name + txtUsername.Name, txtUsername.Text);
            //new cpublic().Registry_Set(this.Name + cboCaLamViec.Name, cboCaLamViec.Text);
        
            #region "   CAPNHAT_TONKHO_HANGHOA"
            Boolean bCAPNHAT_TONKHO_HANGHOA = false;
            Boolean.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["CAPNHAT_TONKHO_HANGHOA"] + "", out bCAPNHAT_TONKHO_HANGHOA);
            if (bCAPNHAT_TONKHO_HANGHOA)
            {
                string sTonkhohanghoa = new cpublic().Registry_Get("UPDATETONKHOHANGHOA") + "";
                if (sTonkhohanghoa != DateTime.Now.ToString("ddMMyyyy"))
                {//neu ngay hon nay cap_nhat roi, thi ko cap_nhat nua
                    new thuvien().SP_Update("sp_tonkho_UPDATE", new string[] { "@ma_khohang" }, new object[] { Variable.ma_khohang });
                    new cpublic().Registry_Set("UPDATETONKHOHANGHOA", DateTime.Now.ToString("ddMMyyyy"));
                }
            }
            #endregion

            #region "   CAPNHAT_TONKHO_NGUYENLIEU"
            Boolean bCAPNHAT_TONKHO_NGUYENLIEU = false;
            Boolean.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["CAPNHAT_TONKHO_NGUYENLIEU"] + "", out bCAPNHAT_TONKHO_NGUYENLIEU);
            if (bCAPNHAT_TONKHO_NGUYENLIEU)
            {
                string sTonkhoNVL = new cpublic().Registry_Get("UPDATETONKHONVL") + "";
                if (sTonkhoNVL != DateTime.Now.ToString("ddMMyyyy"))
                {//neu ngay hon nay cap_nhat roi, thi ko cap_nhat nua
                    new thuvien().SP_Update("sp_tonkhoNGUYEN_LIEU_UPDATE", new string[] { "@ma_khohang" }, new object[] { Variable.ma_khohang });
                    new cpublic().Registry_Set("UPDATETONKHONVL", DateTime.Now.ToString("ddMMyyyy"));
                }
            }
            #endregion
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void txtUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtPwd.Focus();
        }

        private void txtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                cboCaLamViec.Focus();
        }

        private void cboCaLamViec_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                bton_DangNhap.Focus();
        }
    }
}